package tool

import (
	"encoding/json"
	"fmt"
	"net/http"
)

func Resp(w http.ResponseWriter, r *ReplyProto) {
	if w == nil || r == nil {
		fmt.Println("invalid message")
		return
	}

	//Marshal()将数据编码成json字符串
	buf, err := json.Marshal(&r)
	if err != nil {
		_, err := w.Write([]byte(fmt.Sprintf(`{"code":-300,"msg":"%s"}`, err.Error())))
		if err != nil {
			fmt.Println(err.Error())
			return
		}
		fmt.Println(err.Error())
		return
	}

	_, err = w.Write(buf)
	if err != nil {
		fmt.Println(err.Error())
	}
}
